<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  computed: {},
  data() {
    return {
      item: null,
      type: 0,
      title: "",
      form: {
        // 快递公司ID
        companyId: null,
        // 快递单号
        deliveryNo: "",
        // 是否需要发货方式 0否 1是
        isNeedLogistics: 0,
        // 是否需要拆包
        isNeedUnpack: 0,
      },
      // 窗口显示
      boxVisible: false,
      // 物流列表
      logisticsOptions: [],

      newOrderItemLst: [], //改造后的商品数组

      multipleSelection: [], //选中了哪些商品
    };
  },
  methods: {
    // 表格选中项改变
    selectionChange(val) {
      console.log(val);
      this.multipleSelection = val.map((item) => {
        return {
          deliverNum: item.unpackNum,
          orderItemId: item.id,
        };
      });
    },

    // 发货数量改变
    handleChange(val, id_) {
      let index_ = this.multipleSelection.findIndex((item) => {
        return item.orderItemId == id_;
      });

      if (index_ > -1) {
        this.multipleSelection[index_].deliverNum = val;
      }
    },

    // 验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false);
        });
      });
    },

    // 提交
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      if (this.form.isNeedUnpack) {
        this.unPackSubmit();
      } else {
        this.normalSubmit();
      }
    },

    // 正常发货
    async normalSubmit() {
      let _url = "";
      // 发货
      if (this.type == 0) {
        _url = "mall-site-portal/order/deliveryOrder";
      }
      // 修改快递信息
      else if (this.type == 1) {
        _url = "mall-site-portal/order/modifyDelivery";
      }
      let _data = await this.$vFN.Http.post(
        {
          url: _url,

          data: {
            companyId: this.form.companyId,
            deliveryNo: this.form.deliveryNo,
            isNeedLogistics: this.form.isNeedLogistics,
            orderId: this.item.id,
          },
        },
        window
      );
      if (_data._ok) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.editBoxClose();
        this.onSuccess();
      }
    },

    async unPackSubmit() {
      let dataObj_ = {
        isNeedLogistics: this.form.isNeedLogistics,
        orderId: this.item.id,
        unpackItemLst: this.multipleSelection,
      };

      if (this.form.isNeedLogistics) {
        dataObj_.deliveryNo = this.form.deliveryNo;
        dataObj_.companyId = this.form.companyId;
      }

      let res = await this.$vFN.Http.post(
        {
          url: "mall-site-portal/order/unpackOrder",
          data: dataObj_,
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.editBoxClose();
        this.onSuccess();
      }
    },

    // 判断能否选中拆包并发货的商品
    checkSelectable(row) {
      return row.deliverNum != row.buyNum;
    },

    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
    // 失败回调
    onError() {},
    // 关闭窗口
    editBoxClose() {
      this.boxVisible = false;
    },
    // 取消
    editBoxCancel() {
      this.editBoxClose();
    },

    show(e, item) {
      console.log(e, item);
    },
  },
  watch: {},
  async mounted() {
    if (this.type == 0) {
      this.title = "发货";
    } else if (this.type == 1) {
      this.title = "修改快递";
    }

    if (this.item.isUnpack == 1) {
      this.form.isNeedUnpack = 1;
    }

    if (this.item.isNeedLogistics) {
      this.form.isNeedLogistics = 1;
      this.form.companyId = this.item.orderDelivery.companyId;
      this.form.deliveryNo = this.item.orderDelivery.deliveryNo;
    }

    // 改造数组 商品数组里面添加一个发货数量
    this.newOrderItemLst = this.item.orderItemLst.map((item_) => {
      return {
        ...item_,
        unpackNum: item_.buyNum - item_.deliverNum,
      };
    });

    console.error("THIS", this.newOrderItemLst);

    // 数据源 物流列表
    this.logisticsOptions = await this.$vFN.Tools.getSelectListSource(
      {
        url: "mall-site-portal/order/getLogisticsCompany",
        format: false,
      },
      window
    );
    this.boxVisible = true;
  },
  beforeCreate() {},
};
</script>